package com.tensquare.search.service;

import com.tensquare.search.dao.SearchDao;
import com.tensquare.search.pojo.Article;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import utils.IdWorker;

import java.util.List;

@Service
public class SearchService {
    @Autowired
    private SearchDao searchDao;
    @Autowired
    private IdWorker idWorker;
    //1.添加文章
    public void add(Article article) {
        article.setId(idWorker.nextId()+"");
        System.out.println("article = " + article);
        searchDao.save(article);
    }
    //2.关键词查询
    public PageResult<Article> search(String keywords, int page, int size) {
        Page<Article> articlePage = searchDao.findByTitleLikeOrContentLike(keywords, keywords, PageRequest.of(page - 1, size));
        return new PageResult<>(articlePage.getTotalElements(),articlePage.getContent());
    }
}
